﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
/// <summary>
/// Summary description for _Adress
/// </summary>
public class _Adress
{
	public _Adress()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int _id_address { get; set; }
    public int _id_company { get; set; }
    public string _address { get; set; }

    static SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString);

    private static _Adress Fill_Company_address(SqlDataReader dr)
    {
        _Adress Address = new _Adress();

        Address._id_address = (int)dr[0];
        Address._id_company = (int)dr[1];
        Address._address = dr[2].ToString();

        return Address;
    }

    //Atlasam nepieciešamo priekš MyCompanies.aspx Popup
    public static List<_Adress> Select_Company_address(string company_id)
    {
        List<_Adress> address = new List<_Adress>();
        SqlCommand SelCmd = new SqlCommand("SELECT * FROM company_address WHERE id_company=@company_id AND statuss=@status", conn);
        SelCmd.Parameters.Add(new SqlParameter("@company_id", System.Data.SqlDbType.Int)).Value = Convert.ToInt32(company_id);
        SelCmd.Parameters.Add(new SqlParameter("@status", System.Data.SqlDbType.NVarChar,50)).Value = "Active";
        conn.Close();
        conn.Open();
        SqlDataReader dr = SelCmd.ExecuteReader();
        while (dr.Read())
        {
            address.Add(Fill_Company_address(dr));
        }
        dr.Close();
        conn.Close();
        return address;
    }

    //Adreses ievietošana MyCompanies.aspx Popup
    public static _Adress Insert(string id_company, string address)
    {
        _Adress addres = new _Adress();
        SqlCommand AddCmd = new SqlCommand("INSERT INTO company_address (id_company, address, statuss) " + "  VALUES (@id_company, @address, @status);",conn);
        AddCmd.Parameters.Add(new SqlParameter("@id_company", System.Data.SqlDbType.Int)).Value = id_company;
        AddCmd.Parameters.Add(new SqlParameter("@address", System.Data.SqlDbType.NVarChar)).Value = address;
        AddCmd.Parameters.Add(new SqlParameter("@status", System.Data.SqlDbType.NVarChar)).Value = "Active";
        conn.Open();
        AddCmd.ExecuteNonQuery();
        conn.Close();
        return addres;
    }

    //Adreses labošana MyCompanies.aspx Popup
    public static _Adress Update(string id_address, string id_company, string address)
    {
        _Adress addres = new _Adress();
        SqlCommand UpCmd =new SqlCommand("UPDATE company_address SET address=@address, ID_company=@ID_company WHERE ID=@ID", conn);
        UpCmd.Parameters.Add(new SqlParameter("@ID", System.Data.SqlDbType.Int)).Value = id_address;
        UpCmd.Parameters.Add(new SqlParameter("@ID_company", System.Data.SqlDbType.Int)).Value = id_company;
        UpCmd.Parameters.Add(new SqlParameter("@address", System.Data.SqlDbType.NVarChar)).Value = address;
        conn.Open();
        UpCmd.ExecuteNonQuery();
        conn.Close();
        return addres;
    }

    //Adresei nomaina statusu uz DELETED MyCompanies.aspx Popup
    public static _Adress Delete(string id_address)
    {
        _Adress addres = new _Adress();
        SqlCommand UpCmd = new SqlCommand("UPDATE company_address SET statuss=@status WHERE ID=@ID", conn);
        UpCmd.Parameters.Add(new SqlParameter("@ID", System.Data.SqlDbType.Int)).Value = id_address;
        UpCmd.Parameters.Add(new SqlParameter("@status", System.Data.SqlDbType.NVarChar)).Value = "Deleted";
        conn.Open();
        UpCmd.ExecuteNonQuery();
        conn.Close();
        return addres;
    }

    //Atlasa adreses priekš ViewInvoice.aspx un MyCompanies.aspx Popup adreses labošanai
    public static _Adress Select(string id_address)
    {
        _Adress address = new _Adress();
        SqlCommand SelCmd = new SqlCommand("SELECT * FROM company_address WHERE id=@address_id", conn);
        SelCmd.Parameters.Add(new SqlParameter("@address_id", System.Data.SqlDbType.Int)).Value = id_address;
        conn.Open();
        SqlDataReader dr = SelCmd.ExecuteReader();
        while (dr.Read())
        {
            address._id_address = (int)dr[0];
            address._id_company = (int)dr[1];
            address._address = dr[2].ToString();
        }
        dr.Close();
        conn.Close();
        return address;
    }
}